<template>
  <a-modal
    :maskClosable=false
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="归属项目">
          <j-dict-select-tag
            :triggerChange="true"
            v-decorator="[ 'jobProjectCode', {rules:[{required: true,message:'不允许为空'}]}]"
            placeholder="请输入日报项目"
            dictCode="dailyProject"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="归属平台">
          <j-dict-select-tag
            :triggerChange="true"
            v-decorator="[ 'jobPlatformCode', {rules:[{required: true,message:'不允许为空'}]}]"
            placeholder="请输入日报平台"
            dictCode="dailyPlatform"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日报类型">
          <j-dict-select-tag
            :triggerChange="true"
            v-decorator="[ 'jobTypeCode', {rules:[{required: true,message:'不允许为空'}]}]"
            placeholder="请输入日报类型"
            dictCode="dailyType"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="完成状态">
          <j-dict-select-tag :triggerChange="true" v-decorator="[ 'jobStatusCode', {rules:[{required: true,message:'不允许为空'}]}]" placeholder="请输入完成状态" dictCode="dailyStatus"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日报详情">
          <a-textarea placeholder="请输入日报详情" v-decorator="['jobComment', {rules:[{required: true,message:'不允许为空'}]}]" @blur="mantisAnalyse" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="工作量自评">
          <a-input-number :min="0" :max="1" :step="0.01" placeholder="请输入" v-decorator="['workHour',  {rules:[{required: true,message:'不允许为空'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="mantis编号">
          <a-input-number placeholder="请输入mantis编号" v-decorator="['mantisCode', {}]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "OasTimesheetModal",
    components: {
      JDictSelectTag
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        delFlag:{rules: [{ required: true, message: '请输入删除状态（0，正常，1已删除）!' }]},
        },
        url: {
          add: "/oas/oasTimesheet/add",
          edit: "/oas/oasTimesheet/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      mantisAnalyse(){
        this.form.setFieldsValue({mantisCode:parseInt(this.form.getFieldValue('jobComment').match(/^(\d+)[:：]./)[1])})
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','userName','jobProjectCode','workHour','jobPlatformCode','jobTypeCode','jobStatusCode','jobComment','mantisCode'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>